package cn.com.wanywhn.a2017_04_02_bmob_brvah;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.tencent.connect.UserInfo;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONException;
import org.json.JSONObject;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import cn.com.wanywhn.a2017_04_02_bmob_brvah.Bean.MyUser;

public class LoginActivity extends BaseActivityToolBar implements OnClickListener {

    ImageView qqlogin;
    Tencent mtencent;
    Intent intent;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Tencent.onActivityResultData(requestCode, resultCode, data, new BaseUiListener());
        UserInfo info = new UserInfo(this, mtencent.getQQToken());
        info.getUserInfo(new UserinfoListener());
    }

    //QQ登陆
    public class UserinfoListener implements IUiListener {

        @Override
        public void onComplete(Object o) {
            JSONObject mobj = (JSONObject) o;
            try {
                intent = new Intent();
                intent.putExtra("url", mobj.getString("figureurl_2"));
                //TODO 新建用户类，存入头像链接等信息
                intent.setClass(LoginActivity.this, MainActivity.class);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onError(UiError uiError) {

        }

        @Override
        public void onCancel() {

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        qqlogin = (ImageView) findViewById(R.id.QQLogin);
        qqlogin.setOnClickListener(this);
        mtencent = Tencent.createInstance("1106006153", this.getApplicationContext());
        Log.e("testrr", "testrr");
    }


    @Override
    public void onClick(View v) {
        if (!mtencent.isSessionValid()) {
            mtencent.login(this, "all", new BaseUiListener());
        }

    }

    //登陆监听器
    public class BaseUiListener implements IUiListener {

        @Override
        public void onComplete(Object o) {
            Log.d("QQ", o.toString());
            JSONObject mjsonObject = (JSONObject) o;
            try {
                String access_toekn = mjsonObject.getString("access_token");
                String expires_in = mjsonObject.getString("expires_in");
                String openid = mjsonObject.getString("openid");
                //Bmob自带的通过第三方登陆
                MyUser.loginWithAuthData(new MyUser.BmobThirdUserAuth(
                        MyUser.BmobThirdUserAuth.SNS_TYPE_QQ, access_toekn, expires_in, openid), new LogInListener<JSONObject>() {
                    @Override
                    public void done(JSONObject jsonObject, BmobException e) {
                        if (e == null) {
                            Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                            startActivity(intent);
                            LoginActivity.this.finish();
                        } else {
                            Log.d("111111111111111", e.toString());
                            Toast.makeText(LoginActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                mtencent.setAccessToken(access_toekn, expires_in);
                mtencent.setOpenId(openid);

            } catch (JSONException e) {
                e.printStackTrace();
            }


        }


        @Override
        public void onError(UiError uiError) {

            Log.d("QQQQQ", "CODE: " + uiError.errorCode + " msg: " + uiError.errorMessage + " detail: " + uiError.errorDetail);
        }

        @Override
        public void onCancel() {

        }
    }
}

